package edu.spec.sms.model;

import edu.spec.sms.service.iface.IClazzService;
import edu.spec.sys.model.anno.Excel;
import edu.spec.sys.model.enumeration.AttendStatus;
import edu.spec.sys.model.enumeration.Gender;
import edu.spec.sys.model.enumeration.Status;
import edu.spec.sys.web.BeanFactoryContext;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "t_student", uniqueConstraints = {@UniqueConstraint(columnNames = {"xh", "open_id"})})
public class Student {
	private int id;
	private String openId;
//	private BindStatus bindStatus;
	@Excel(columnName="姓名",exportWidth=20,exportConvert=false,importConvert=false)
	private String name;
	@Excel(columnName="性别", exportWidth=10, exportConvert=true, importConvert=true)
	private Gender xb;
	@Excel(columnName="学号",exportWidth=20,exportConvert=false,importConvert=false)
	private String xh;
	@Excel(columnName="身份证号",exportWidth=20,exportConvert=false,importConvert=false)
	private String sfzh;
	@Excel(columnName="班级名称",exportWidth=20,exportConvert=false,importConvert=true)
	private String bjmc;
	private int bjid;
	private Clazz clazz;
	private String xbmc;
	private int xbid;
	private Status status;
	private Date createDate;
	private Date updateDate;
	private String des;
    @Excel(columnName="参加状态",exportWidth=20,exportConvert=true,importConvert=false)
	private AttendStatus attendStatus;
    // 1表示“已经签到”，0表示“未签到”
    private Integer signInStatus;

	@Id
	@GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "open_id")
	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

//	@Column(name = "bind_status")
//    public BindStatus getBindStatus() {
//        return bindStatus;
//    }
//
//    public void setBindStatus(BindStatus bindStatus) {
//        this.bindStatus = bindStatus;
//    }

    public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getXh() {
		return xh;
	}

	public void setXh(String xh) {
		this.xh = xh;
	}

    public String getSfzh() {
        return sfzh;
    }

    public void setSfzh(String sfzh) {
        this.sfzh = sfzh;
    }

    public Gender getXb() {
		return xb;
	}

	public void setXb(Gender xb) {
		this.xb = xb;
	}

    public String obtainXbConvert(){
        if(Gender.MALE.equals(xb)){
            return "男";
        }else if (Gender.FEMALE.equals(xb)){
            return "女";
        }else {
            return "未知";
        }
    }

    public void setXbConvert(String xb){
	    if("男".equals(xb)){
	        this.xb = Gender.MALE;
        }else if("女".equals(xb)){
	        this.xb = Gender.FEMALE;
        }
    }

	public String getBjmc() {
		return bjmc;
	}

	public void setBjmc(String bjmc) {
		this.bjmc = bjmc;
	}

	public void setBjmcConvert(String bjmc){
        IClazzService clazzService = (IClazzService)BeanFactoryContext.getService("clazzService");
        Clazz c = clazzService.loadByName(bjmc);
        this.bjmc = bjmc;
        this.bjid = c.getId();
    }

	public int getBjid() {
		return bjid;
	}

	public void setBjid(int bjid) {
		this.bjid = bjid;
	}

	@ManyToOne
	@JoinColumn(name = "bjid", insertable=false, updatable=false)
	public Clazz getClazz() {
		return clazz;
	}

	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}

	public String getXbmc() {
		return xbmc;
	}

	public void setXbmc(String xbmc) {
		this.xbmc = xbmc;
	}

	public int getXbid() {
		return xbid;
	}

	public void setXbid(int xbid) {
		this.xbid = xbid;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	@Column(name = "create_date")
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@Column(name = "update_date")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	public String getDes() {
		return des;
	}

	public void setDes(String des) {
		this.des = des;
	}

    @Transient
    public AttendStatus getAttendStatus() {
        return attendStatus;
    }

	public void setAttendStatus(AttendStatus attendStatus) {
		this.attendStatus = attendStatus;
	}

	// 对于get开头的方法，jpa会自动去找set方法，并在数据库建表，完成数据库映射。故需@Transient使用忽略，或者修改get为obtain
	public String obtainAttendStatusConvert(){
		String str = "";
		if(AttendStatus.UNENROLL.equals(attendStatus)){
			str = "未报名";
		}else if (AttendStatus.UNSIGNIN.equals(attendStatus)){
			str = "未签到";
		}else  if (AttendStatus.ATTEND.equals(attendStatus)){
			str = "正常参加";
		}
		return str;
	}

	public Student() {
	}

    @Transient
    public Integer getSignInStatus() {
        return signInStatus;
    }

    public void setSignInStatus(Integer signInStatus) {
        this.signInStatus = signInStatus;
    }

    @Override
	public String toString() {
		return "Student{" +
				"openId='" + openId + '\'' +
				", name='" + name + '\'' +
				", xh='" + xh + '\'' +
				", sfzh='" + sfzh + '\'' +
				'}';
	}
}
